package com.jaiku.api
{
	import com.jaiku.api.objects.JaikuComment;
	import com.jaiku.api.objects.JaikuNote;
	import com.jaiku.api.objects.JaikuUser;
	import com.jaiku.api.results.JaikuNotesResult;
	import com.jaiku.api.results.JaikuUsersResult;
		
	public final class JaikuResultParser
	{
		public static function parseNotes(resultData:Object, comments:Boolean):JaikuNotesResult
		{
			var jaikuNotesResult:JaikuNotesResult = new JaikuNotesResult();
			var notes:Array = new Array();
			
			jaikuNotesResult.url = resultData.url;
			jaikuNotesResult.title = resultData.title;
			
			for each(var rawNote:Object in resultData.stream){
				if (Boolean(rawNote.id))
				{
					var note:JaikuNote = parseJaikuNote(rawNote);
					notes.push(note);
				}
				else if (comments)
				{
					var comment:JaikuComment = parseJaikuComment(rawNote);
					notes.push(comment);	
				}
			}
			jaikuNotesResult.notes = notes;
			
			return jaikuNotesResult;
		}
		public static function parseNote(resultData:Object):JaikuNotesResult
		{
			var jaikuNotesResult:JaikuNotesResult = new JaikuNotesResult();
			var notes:Array = new Array();
			
			var note:JaikuNote = parseJaikuNote(resultData);

			if(resultData.comments.length > 0)
			{
				note.comments_count = resultData.comments.length;
				note.comments = new Array();
				for each(var comment:Object in resultData.comments)
				{
					var noteComment:JaikuNote = parseJaikuNote(comment);
					note.comments.push(noteComment);
				}
			}

			notes.push(note);	
			jaikuNotesResult.notes = notes;
			
			return jaikuNotesResult;
		}		
		public static function parseProfile(resultData:Object):JaikuUsersResult
		{
			var jaikuUsersResult:JaikuUsersResult = new JaikuUsersResult();
			
			jaikuUsersResult.user = parseJaikuUser(resultData);
			
			return jaikuUsersResult;
		}
		private static function parseJaikuNote(rawNote:Object):JaikuNote
		{
			var note:JaikuNote  = new JaikuNote();
			
			note.id = rawNote.id;
			note.title = rawNote.title;
			note.content = rawNote.content;
			note.icon = rawNote.icon;
			note.url = rawNote.url;
			note.created_at = rawNote.created_at;
			note.created_at_relative = rawNote.created_at_relative;
			
			if (typeof rawNote.comments == "string")
				note.comments_count = rawNote.comments;	

			note.location = rawNote.location;
			note.user = parseJaikuUser(rawNote.user);
					
			return note; 
		}
		private static function parseJaikuComment(rawComment:Object):JaikuComment
		{
			var comment:JaikuComment  = new JaikuComment();
			
			comment.comment_id = rawComment.comment_id;
			comment.title = rawComment.title;
			comment.entry_title = rawComment.entry_title;
			comment.content = rawComment.content;
			comment.pretty_content = rawComment.pretty_content;
			comment.url = rawComment.url;
			comment.created_at = rawComment.created_at;
			comment.created_at_relative = rawComment.created_at_relative;
			comment.user = parseJaikuUser(rawComment.user);
			
			return comment; 
		}
		private static function parseJaikuUser(rawUser:Object):JaikuUser
		{
			var user:JaikuUser = new JaikuUser();
			
			user.avatar = rawUser.avatar;
			user.first_name = rawUser.first_name;
			user.last_name = rawUser.last_name;
			user.nick = rawUser.nick;
			user.url = rawUser.url;
			if (Boolean(rawUser.contacts))
			{
				user.contacts = new Array();
				for each(var contactUser:Object in rawUser.contacts)
				{
					user.contacts.push(parseJaikuUser(contactUser));
				}
			} 
			
			return user; 
		}					
	}
}